﻿using log4net;
using log4net.Config;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

// Configure log4net using the .config file
[assembly: XmlConfigurator(Watch = true)]
// This will cause log4net to look for a configuration file
// called [App].config in the application base directory

namespace Shininglore.XAuth
{
    class Program
    {
        // Create a logger for use in this class
        private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        static void Main(string[] args)
        {
            if (args.Length > 0 && (args[0] == "-h" || args[0] == "/h"))
            {
                Console.WriteLine("Shininglore XAuthenticator v" + Assembly.GetEntryAssembly().ImageRuntimeVersion);
                Console.WriteLine("xauth -i\tInstall hash table into Shininglore database.");
                Console.WriteLine("xauth -h\tDisplay usage.");
                Console.WriteLine("xauth\tRunning authentication service.");
            }
            else if (args.Length > 0 && (args[0] == "-i" || args[0] == "/i"))
            {
                log.Info("Install hash database...");
                SetupManager.GetInstance().Run();
                log.Info("Finish installation hash database");
            }
            else
            {
                log.Info("Starting Shininglore Online authentication service...");
                AuthManager.GetInstance().Run();
                log.Info("Stopping Shininglore Online authentication service");
            }
        }

        public static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            log.Fatal("Unhandled error occurred and application is terminating.");

            Exception ex = e.ExceptionObject as Exception;
            if (ex != null)
                log.Fatal(ex);
        }
    }
}
